package com.example.hao.guidancedemo;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.hao.guidancedemo.Adapter.MyViewPageAdapter;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private ViewPager vp;
    private RadioGroup rg;
    private int widthPixels;
    private int indicatorWidth;
    private View view_indicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        widthPixels = metrics.widthPixels;
        Log.d(TAG, "onCreate:widthPixels "+widthPixels);
        indicatorWidth = widthPixels / 5;
        Log.d(TAG, "onCreate:indicatorWidth "+indicatorWidth);
        initView();
        initListener();
        lodData();
    }

    private void initView() {
        vp = findViewById(R.id.vp);
        rg = findViewById(R.id.rg);
        view_indicator = findViewById(R.id.indicator);

    }
    private void initListener() {
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                view_indicator.setX((position % 4 + positionOffset) * indicatorWidth);
                if (positionOffset > 0.5f) {
                    view_indicator.setScaleX(3 - positionOffset * 2);
                } else {
                    view_indicator.setScaleX(1 + positionOffset * 2);
                }

            }

            @Override
            public void onPageSelected(int position) {

                RadioButton childAt = (RadioButton) rg.getChildAt(position);
                childAt.setChecked(true);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void lodData() {

        int[] array ={R.mipmap.g1,R.mipmap.g2,R.mipmap.g3,R.mipmap.g4,R.mipmap.g5};
        MyViewPageAdapter pageAdapter = new MyViewPageAdapter(this,array);
        vp.setAdapter(pageAdapter);
    }

}
